package com.timemanager.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Chronometer;
import android.widget.TextView;
import android.widget.Toast;

import com.timemanager.R;
import com.timemanager.application.MyApplication;
import com.timemanager.db.TimeManagerDB;
import com.timemanager.model.EventTime;
import com.timemanager.util.Utility;


public class StartEventActivity extends AppCompatActivity {

    private TextView titleText;
    private String eventName;
    private int eventId;
    private EventTime eventTime = new EventTime();
    private Chronometer chronometer = null;
    long timer = 0;
    private TimeManagerDB timeManagerDB;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start_event);
        titleText = (TextView) findViewById(R.id.title_text);
        eventName = getIntent().getStringExtra("event_name");
        eventId = getIntent().getIntExtra("event_id", 0);
        titleText.setText("开始活动：" + eventName);
        timeManagerDB = TimeManagerDB.getInstance(StartEventActivity.this);
        chronometer = (Chronometer) findViewById(R.id.chronometer);
        chronometer.setFormat("%s");
    }

    public void onStart(View view)
    {
        chronometer.setBase(SystemClock.elapsedRealtime() - timer);
        chronometer.start();
    }

    public void onStop(View view)
    {
        chronometer.stop();
        timer = SystemClock.elapsedRealtime() - chronometer.getBase();
        showStopDialog();
//        Toast.makeText(StartEventActivity.this, timer / 1000 + "", Toast.LENGTH_SHORT).show();
    }

    public void onReset(View view)
    {
        chronometer.setBase(SystemClock.elapsedRealtime());
    }

    private void showStopDialog() {
        AlertDialog.Builder builder= new AlertDialog.Builder(StartEventActivity.this);
        builder.setTitle("暂停");
        builder.setMessage("你进行了" + Utility.formatTime(timer) + eventName + "活动。");
        builder.setCancelable(false);
        builder.setPositiveButton("完成保存", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                eventTime.setElapsedTime(timer);
                eventTime.setEventId(eventId);
                if (timeManagerDB.saveEventTime(eventTime) > 0) {
                    Toast.makeText(MyApplication.getContext(), "保存成功！", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(StartEventActivity.this, MainActivity.class);
                    startActivity(intent);
                }
            }
        });
        builder.setNegativeButton("返回继续", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                chronometer.setBase(SystemClock.elapsedRealtime() - timer);
                chronometer.start();
            }
        });
        builder.show();
    }

}
